package com.dizner.androidcarclient;

import android.os.SystemClock;
import android.util.Log;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class AutoPlayManager {
    private static final String TAG = "AutoPlayManager";
    private static AutoPlayManager autoPlayManager;
    private static final long def_miles = 1000;//控制消息发送默认间隔时间
    private boolean enableForward = true;
    private boolean enableLeft = true;
    private boolean enableRight = true;
    private boolean enableBack = true;
    private boolean isForward;
    private boolean isRight;
    private boolean isLeft;
    private boolean isBack;

    private boolean autoRunning;

    private AutoPlayManager() {
        if (forwardThread == null) {
            forwardThread = new Thread() {
                @Override
                public void run() {
                    while (true) {
                        if (isAutoRunning()) {
                            Log.i(TAG, "正在执行...");
                            SystemClock.sleep(1000);
                        }
                    }
                }
            };
        }
    }

    public static AutoPlayManager getInstance(Object motorHAT) {
        if (autoPlayManager == null) {
            autoPlayManager = new AutoPlayManager();
        }
        return autoPlayManager;
    }


    public void run() {
        autoRunning = true;
        if (forwardThread != null && !forwardThread.isAlive()) {
            forwardThread.start();
        }
    }

    private Thread forwardThread;


    public boolean isAutoRunning() {
        return autoRunning;
    }

    public void stop() {
        isForward = false;
        isRight = false;
        isLeft = false;
        isBack = false;
        autoRunning = false;
//        if (forwardThread != null) {
//            forwardThread.interrupt();
//        }
    }
}
